package org.wattdepot.cli.commands;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * This class tests the power time stamp command of the 
 * CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandPowerTimeStamp {
  String cr = System.getProperty("line.separator");
  
  /**
   * Test normal operation of the PowerTimeStamp command.
   */
  @Test
  public void testPowerTimeStamp() {
    // Create a new power time stamp command object.
    PowerTimeStampCommand command = new PowerTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_2", "2009-10-31T23:45:00.000-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Ready the output that we know is true
    String knownOutput = "57.0 MegaWatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    int result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known power with good timestamp", result, 0);

    // Create a new List to store the args
    args = Arrays.asList( "SIM_WAIAU_8", "2009-11-15T12:13:00.000-10:00" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    knownOutput = "88.0 MegaWatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known Waiau #8 power with good timestamp", result, 0);

    // Create a new List to store the args
    args = Arrays.asList( "SIM_WAIAU", "2009-11-15T12:13:00.000-10:00" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    knownOutput = "176.0 MegaWatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known Waiau power with good timestamp", result, 0);

    // Create a new List to store the args
    args = Arrays.asList( "SIM_OAHU_GRID", "2009-11-15T12:13:00.000-10:00" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    knownOutput = "649.6 MegaWatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known Oahu power with good timestamp", result, 0);
  }

  /**
   * Test bad timestamp detection for the powerTimeStamp command.
   */
  @Test
  public void testPowerTimeStampBadTime() {
    // Create a new power time stamp command object.
    PowerTimeStampCommand command = new PowerTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_2", "2-10-31T23:40-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Check to see if the correct error returned
    assertTrue("Checking for correct response to bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));
  }

  /**
   * Test bad source detection for the powerTimeStamp command.
   */
  @Test
  public void testPowerTimeStampBadSource() {
    // Create a new power time stamp command object.
    PowerTimeStampCommand command = new PowerTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "BLAH", "2009-10-31T23:45:00.000-10:00" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Check to see if the correct error returned
    assertTrue("Checking for correct response to bad source", sourceOutput
        .contains(WattDepotCliCommand.resourceNotFound));
  }
  
  /**
   * Tests to see if the PowerTimeStamp command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new power time stamp command object.
    PowerTimeStampCommand command = new PowerTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1", "2", "3");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess PowerTimeStamp argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
  
  /**
   * Tests to see if the PowerTimeStamp command can handle not enough arguments.
   */
  @Test
  public void testFewArguments() {
    // Create a new power time stamp command object.
    PowerTimeStampCommand command = new PowerTimeStampCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking PowerTimeStamp handling not enough arguments", 
        sourceOutput.contains(WattDepotCliCommand.missingArguments));
  }
}
